<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="评论主题" prop="topic">
        <el-select
          v-model="queryParams.topic"
          placeholder="请选择评论主题"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.blog_comment_topic"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['blog:comment:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      border
      stripe
      v-loading="loading"
      :data="commentList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="评论人头像"
        align="center"
        prop="avatar"
        width="100px"
      >
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatar" :width="40" :height="40" />
        </template>
      </el-table-column>
      <el-table-column label="评论人" align="center" prop="nickName" />
      <el-table-column label="回复" align="center" prop="answerNickName" />
      <el-table-column
        label="评论内容"
        align="center"
        prop="commentContent"
      />
      <el-table-column label="评论主题" align="center" prop="topic">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.blog_comment_topic"
            :value="scope.row.topic"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="主题内容"
        align="center"
        prop="topicContent"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="ip属地" align="center" prop="ipSource" />
      <el-table-column label="点赞数量" align="center" prop="likedCount" />
      <el-table-column
        label="评论时间"
        align="center"
        prop="createTime"
        width="160px"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['blog:comment:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listComment, delComment } from "@/api/blog/comment";

export default {
  name: "Comment",
  dicts: ["blog_comment_topic"],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 博客评论表格数据
      commentList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        topic: undefined,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询博客评论列表 */
    getList() {
      this.loading = true;
      listComment(this.queryParams).then((response) => {
        this.commentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.commentId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const commentIds = row.commentId || this.ids;
      this.$modal
        .confirm('此操作会删除所选评论的子级评论哦，是否确认删除博客评论编号为"' + commentIds + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delComment(commentIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
  },
};
</script>
